package com.duqc.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName IntersectionUtil
 * @Description IntersectionUtil
 * @Author duqc
 * @Date 2023/2/9
 **/
public class IntersectionUtil {

    /**
     * 从有值的list中取交集
     *
     * @param lists
     * @return
     */
    public static List<String> getIntersection(List<List<String>> lists) {
        if (lists == null || lists.size() == 0) {
            return null;
        }
        ArrayList<List<String>> arrayList = new ArrayList<>(lists);
        boolean b = false;
        for (List<String> list : arrayList) {
            if (list == null || list.size() == 0) {
                b = true;
                break;
            }
        }
        if (b) {
            return null;
        }
        if (arrayList.size() == 0) {
            return null;
        }
        List<String> intersection = arrayList.get(0);
        if (arrayList.size() == 1) {
            return intersection;
        }
        for (int i = 1; i < arrayList.size(); i++) {
            intersection.retainAll(arrayList.get(i));
        }
        return intersection;
    }

}
